package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.PopwindowPickHallBinding;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.pick.PickInquireByLetterActivity;
import com.sgb.kjwl.view.ui.activity.pick.ReportActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickHomeHotChildHeadAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/8/19
 * @Description: 工程分包图查PopWindow
 */
public class MapSubcontractPopWindow extends BasePopWindow implements View.OnClickListener{
    PopwindowPickHallBinding mBinding;

    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;

    TenderListEntity mEntity;

    BtnClickListener btnClickListener;

    public void setBtnClickListener(BtnClickListener btnClickListener) {
        this.btnClickListener = btnClickListener;
    }

    public MapSubcontractPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_pick_hall;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding= (PopwindowPickHallBinding) holder.getmBinding();
        mBinding.setOnClick(this);

    }


    public void setData(TenderListEntity entity){
        mEntity=entity;
        if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
            mBinding.childRv.setVisibility(View.GONE);
            mBinding.noList.setVisibility(View.VISIBLE);
        } else {
            mBinding.childRv.setVisibility(View.VISIBLE);
            mBinding.noList.setVisibility(View.GONE);
            pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(mContext, entity.getGoodsHeaders(), entity.getGoodsLists());
            mBinding.childRv.setLayoutManager(new LinearLayoutManager(mContext));
            mBinding.childRv.setAdapter(pickHomeHotChildHeadAdapter);
            mBinding.name.setText(entity.getName());
            mBinding.tvType2.setText(entity.getTypeName());
            // 招标方式：1、资格预审；2、资格后审；3、报价即报名
            if (null == entity.getVerticalTypeValue()){
                entity.setVerticalTypeValue(0);
            }
            if(entity.getVerticalTypeValue() > 0){
                mBinding.tvType.setVisibility(View.VISIBLE);
            }else{
                mBinding.tvType.setVisibility(View.GONE);
            }
            if (1 == entity.getVerticalTypeValue()) {
                mBinding.tvType.setText("资格预审");
                mBinding.tvType.setBackgroundResource(R.drawable.bg_purple);
            } else if (2 == entity.getVerticalTypeValue()){
                mBinding.tvType.setText("资格后审");
                mBinding.tvType.setBackgroundResource(R.drawable.bg_orange);
            }else if (3 == entity.getVerticalTypeValue()){
                mBinding.tvType.setBackgroundResource(R.drawable.bg_price);
                mBinding.tvType.setText("报名即报价");
            }
            switch (entity.getStatus()) {
                case "2":
                case "6":
                    mBinding.tvEndTime.setText(String.format("投标结束时间：%s", TextUtils.isEmpty(entity.getEndTime()) ? "" : entity.getEndTime()));
                    break;
                case "4":
                    mBinding.tvEndTime.setText(String.format("定标时间：%s", TextUtils.isEmpty(entity.getUpdated()) ? "" : entity.getUpdated()));
                    break;
                case "9":
                    mBinding.tvEndTime.setText(String.format("报名结束时间：%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? "" : entity.getApplyEndTime()));
                    break;
                case "3":
                    mBinding.tvEndTime.setText(String.format("开标时间：%s", TextUtils.isEmpty(entity.getBidTime()) ? "" : entity.getBidTime()));
                    break;
                case "1":
                    mBinding.tvEndTime.setText(String.format("投标开始时间：%s", TextUtils.isEmpty(entity.getStartTime()) ? "" : entity.getStartTime()));
                    break;
                case "8":
                    mBinding.tvEndTime.setText(String.format("报名开始时间：%s", TextUtils.isEmpty(entity.getEndTime()) ? "" : entity.getEndTime()));
                    break;
              /*  case "5":
                    mBinding.tvEndTime.setText(String.format("废标时间：%s", TextUtils.isEmpty(entity.getUpdated()) ? "" : entity.getUpdated()));
                    break;
                case "7":
                    mBinding.tvEndTime.setText(String.format("流标时间：%s", TextUtils.isEmpty(entity.getUpdated()) ? "" : entity.getUpdated()));
                    break;*/
            }
            //新增 “招标轮次”     “报名中”9、“待招标”1状态不显示，其他状态显示    接口判断
            if (TextUtils.isEmpty(entity.getRotation()) || "0".equals(entity.getRotation()) || "1".equals(entity.getStatus()) || "8".equals(entity.getStatus()) || "9".equals(entity.getStatus())) {
                mBinding.tvWheelNum.setVisibility(View.GONE);
            } else {
                mBinding.tvWheelNum.setVisibility(View.VISIBLE);
                mBinding.tvWheelNum.setText("第" + entity.getRotation() + "轮");
            }
        }
        if (mEntity.getUserFocusFlag() == 0) {
            mBinding.imageUserFocus.setImageResource(R.mipmap.guanzhu_pick);
            mBinding.tvUserFocus.setText("关注");
            mBinding.tvUserFocus.setTextColor(mContext.getResources().getColor(R.color.color_tab_one));
        } else {
            mBinding.imageUserFocus.setImageResource(R.mipmap.yiguanzhu_pick);
            mBinding.tvUserFocus.setText("已关注");
            mBinding.tvUserFocus.setTextColor(mContext.getResources().getColor(R.color.color_main));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_see_detail:
                if(btnClickListener!=null){
                    btnClickListener.toDetail(mEntity);
                }

                break;
            case R.id.tv_report:
                ReportActivity.start(mContext, mEntity.getId(), "1");
                break;
            case R.id.tv_inquire_by_letter:
                if (UserOperating.getInstance().isNotLogged(mContext)) {
                    return;
                }
                checkAllowTenderQuestion(mEntity.getId(), Integer.valueOf(mEntity.getType()));
                break;
            case R.id.line_user_focus:
                if (UserOperating.getInstance().isNotLogged(mContext)) {
                    return;
                }
                if(btnClickListener!=null){
                    btnClickListener.follow(mEntity);
                }
                break;
            case R.id.tv_contact_service:
                if (UserOperating.getInstance().isNotLogged(mContext)) {
                    return;
                }
                if(TextUtils.isEmpty(mEntity.getCustomerServiceImAccount())){
                    MToast.showToast(mContext,"招标方未设置客服人员，不支持交流。");
                    return;
                }
                if(btnClickListener!=null){
                    btnClickListener.toIm(mEntity.getCustomerServiceImAccount());
                }
                break;


        }

    }
    public void checkAllowTenderQuestion(String tenderNo,int tenderingType) {
        DialogHelper.showProgressDialog(mContext, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("tenderNo", tenderNo);
        APIManager.getInstance().getPickAPI().checkAllowTenderQuestion(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    checkAllowDialog(response.body().getMsg());
                } else {
                    PickInquireByLetterActivity.start(mContext, tenderNo, "activity",tenderingType);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
    public void checkAllowDialog(String msg) {
        View view = View.inflate(mContext, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(mContext, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);

        tv_logout_cancel.setVisibility(View.GONE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("提示");

        tv_message.setText(msg);

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
    public interface BtnClickListener {
        void follow(TenderListEntity entity);

        void toIm(String customerServiceImAccount);

        void toDetail(TenderListEntity entity);

    }
}
